#import "Common.h"
#import "LayerPause.h"
#import "LayerSelMode.h"
#import "LayerGame.h"
#import "LayerMainMenu.h"

CGPoint sBtnPos[3] = { ccp(120,30), ccp(240,30), ccp(360,30) };
NSString* sBtnLabel[3] = { @"BtnQuit", @"BtnRestart", @"BtnContinue" };
CGPoint sStartPos = ccp(70,220);

@implementation LayerPause

-(id) init
{
	if ((self = [super init]))	{
		[self addChild:[CCLayerColor layerWithColor:ccc4(0,0,0,200)]];
		
		// GameMode
   		CCLabelTTF* labelMode = [CCLabelTTF labelWithString:@"" fontName:gFontName fontSize:30];
		labelMode.string = NSLocalizedString((gCurScore.gameMode == GM_Time) ? @"GMClassic" : @"GMSurvival",nil);
		labelMode.anchorPoint = gAnchorRight;
		labelMode.color = ccGREEN;
		labelMode.position = ccp(470,300);
		[self addChild:labelMode];
		
		// Enemies
		CCLabelTTF* labelEnemies = [CCLabelTTF labelWithString:NSLocalizedString(@"Invaders",nil) fontName:gFontName fontSize:20];
		labelEnemies.color = ccYELLOW;
		labelEnemies.anchorPoint = gAnchorLeft;
		labelEnemies.position = ccp(80,260);
		[self addChild:labelEnemies];

		for( int i = 0; i < INVADERS_COUNT; i++) {
			InvaderData& id = gInvadersData[i];
			CCSprite* invader = [CCSprite spriteWithSpriteFrame:[id.frameArr objectAtIndex:0]];
			int x = sStartPos.x+(i%3)*120;
			int y = sStartPos.y-(i/3)*40;
			invader.position = ccp(x,y);
			invader.scale = 0.8;
			invader.anchorPoint = gAnchorLeft;
			[self addChild:invader];

			CCLabelTTF* labelCount = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"x %d", gCurScore.invadersCount[i]]
														fontName:gFontName
														fontSize:18];
			labelCount.color = ccYELLOW;
			labelCount.anchorPoint = gAnchorLeft;
			labelCount.position = ccp(x+45,y);
			[self addChild:labelCount];
		}
		
		CCTexture2D* btnNml = [[CCTextureCache sharedTextureCache] addImage:@"btn-02.png"];
		CCTexture2D* btnNmlDown = [[CCTextureCache sharedTextureCache] addImage:@"btn-02-s.png"];

		CCSprite *item, *itemDown;
		CCMenuItemSprite* menuItem;
		CCMenu* menu = [CCMenu menuWithItems:NULL];
		menu.position = CGPointZero;
		[self addChild:menu];
		
		for(int i = 0; i < 3; i++) {
			item = [CCSprite spriteWithTexture:btnNml];
			itemDown = [CCSprite spriteWithTexture:btnNmlDown];
			menuItem = [CCMenuItemSprite itemFromNormalSprite:item
											   selectedSprite:itemDown
													   target:self
													 selector:@selector(callMenu:)];
			menuItem.position = sBtnPos[i];
			CCLabelTTF* labelItem = [CCLabelTTF labelWithString:NSLocalizedString(sBtnLabel[i],nil)
													   fontName:gFontName
													   fontSize:16];
			labelItem.position = ccp(item.contentSize.width/2,item.contentSize.height/2+2);
			[menuItem addChild:labelItem];
			[menu addChild:menuItem z:i+1000];
		}
	}
	return self;
}

-(void) callMenu:(CCMenuItem*)sender
{
	int id = [sender zOrder];
	[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];

	switch(id) {
		case 1000:
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneSelMode node]]];
			[[SimpleAudioEngine sharedEngine] playBackgroundMusic:MUSIC_BG1 loop:true];
			break;
		case 1001:
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[[SceneGame alloc] initWithMode:gCurScore.gameMode]]];
			break;
		case 1002: {
			CCScene* scene = [[CCDirector sharedDirector] runningScene];
			for(CCLayer* layer in scene.children)
				if(![layer isRunning])
					[layer onEnter];
			[scene removeChild:self cleanup:true];
			break;
		}
		case 1003:
			break;
	}
}

@end
